package erChaShuFuXi;

import java.util.Stack;

public class erChaShuLianXiTi2 {
      public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
      }
      //判断是否是对称二叉树
      public boolean isSymmetric(TreeNode root) {
          if(root==null){
              return true;
          }
          return isSymmetricChild(root.left,root.right);
      }
      public boolean isSymmetricChild(TreeNode p1,TreeNode p2){
        if((p1==null&&p2!=null)||(p1!=null&&p2==null)){
            return false;
        }
        if(p1==null&&p2==null){
            return true;
        }
        if(p1.val!=p2.val){
            return false;
        }
        return isSymmetricChild(p1.left,p2.right)&&isSymmetricChild(p1.right,p2.left);
    }

    //二叉树的遍历
    public int count=0;
    public TreeNode createTree(String str){
        TreeNode root=null;
        if (str.charAt(count) != '#') {
            root=new TreeNode(str.charAt(count));
            count++;
            root.left=createTree(str);
            root.right=createTree(str);
        }
        else {
            count++;
        }
        return root;
    }

    public TreeNode inOrder(TreeNode root){
        if(root==null){
            return null;
        }
        inOrder(root.left);
        System.out.println(root.val+" ");
        inOrder(root.right);
        return root;
    }

    public void inOrderFeiDiGui(TreeNode root){
        if(root==null){
            return;
        }
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty()){
            while (cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            System.out.println(top.val+' ');
            cur=top.right;
        }
    }

}
